
#define RUNTIME_DBG_LVL 0
#define _GNU_SOURCE
#include <pthreadalt.h>

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <dbg.h>
#include "dblk_test.h"

#include <ddblk/apds.h>


static tstack_t apdstinfo_tlsstk;
static TLS_INITIAL apdstls_t *apdstinfo_tls;
DEF_APDSTINFO_TLS(&apdstinfo_tlsstk, &apdstinfo_tls, malloc, 4096, mp_coreid)
static TLS_INITIAL spobj_t *lknode_tls;
DEF_LOCKNODE_SPSLAB(splknode, &lknode_tls, HDEF_CACHE_LINE_BYTES, 16384)

DEF_COMBINE(ccsync, ccsync, OUFUN_OF(apdstls), OUFUN_OF(spin_wait), OUFUN_OF(spslablknode))
DEF_COMBINE(dsmsync, dsmsync, OUFUN_OF(apdstls), OUFUN_OF(spin_wait), OUFUN_OF(spslablknode))

/* 先定义 hsync */
/* DEF_HSYNC_STATE(hsync_state, APDS_NUMA_NR, yield_spin)
DEF_HSYNC(hsync, APDS_NUMA_NR, APDS_GET_NUMANODES, yield_spin, &hsync_state.hsync_glk, OUFUN_OF(hsync_state))
DEF_COMBINE(hsync, hsync, OUFUN_OF(apdstls), OUFUN_OF(spin_wait), OUFUN_OF(spslablknode))
 */
DEF_HSYNC(hsync, APDS_NUMA_NR, APDS_GET_NUMANODES, spin)
DEF_COMBINE(hsync, hsync, OUFUN_OF(apdstls), OUFUN_OF(spin_wait), OUFUN_OF(spslablknode))

/* flat */
DEF_COMBINE(flat, flat, OUFUN_OF(apdstls), OUFUN_OF(spin_wait), OUFUN_OF(spslablknode))
/* gflat */

#include <pthreadalt/pthreadalt_init.h>
DEF_PTHREADALT_INIT
DEF_SYSINIT(apdstls)
DEF_CFLOWINIT(apdstls)
DEF_CFLOWFIN(apdstls, splknode)
DEF_SYSFIN(__omega)

static apdsret_t test_func(APDSFUNC_ARGLIST){
	static word_t v;
	PARAM_USED(arg1, arg2, arg3);
	v += arg0;
	return v;
}

void test_ccsync(){
	APDS_ARGS(args, 1);
	apdsret_t res = SIMPLE_COMBINETEST(ccsync, test_func, args);
	assert(res == 1);
}

void test_dsmsync(){
	APDS_ARGS(args, 2);
	apdsret_t res = SIMPLE_COMBINETEST(dsmsync, test_func, args);
	assert(res == 3);
}

void test_hsync(){
	APDS_ARGS(args, 3);
	apdsret_t res = SIMPLE_COMBINETEST(hsync, test_func, args);
	assert(res == 6);
}

void test_flat(){
	APDS_ARGS(args, 4);
	apdsret_t res = SIMPLE_COMBINETEST(flat, test_func, args);
	assert(res == 10);
}


int main(){
	test_ccsync();
	test_dsmsync();
	test_hsync();
	test_flat();
	return 0;
}
